// 包裹列表页的模拟数据
const packages = [
  { trackingNo: 'TRK001', fromCampus: 'Campus A', toCampus: 'Campus B', status: 'Pending pickup' },
  { trackingNo: 'TRK002', fromCampus: 'Campus C', toCampus: 'Campus D', status: 'Picked up' },
  { trackingNo: 'TRK003', fromCampus: 'Campus E', toCampus: 'Campus F', status: 'Pending transit' },
  { trackingNo: 'TRK004', fromCampus: 'Campus G', toCampus: 'Campus H', status: 'In transit' },
  { trackingNo: 'TRK005', fromCampus: 'Campus I', toCampus: 'Campus J', status: 'Pending delivery' },
  { trackingNo: 'TRK006', fromCampus: 'Campus K', toCampus: 'Campus L', status: 'Delivering' },
  { trackingNo: 'TRK007', fromCampus: 'Campus M', toCampus: 'Campus N', status: 'Signed' },
  { trackingNo: 'TRK008', fromCampus: 'Campus O', toCampus: 'Campus P', status: 'Pending pickup' },
  { trackingNo: 'TRK009', fromCampus: 'Campus Q', toCampus: 'Campus R', status: 'Picked up' },
  { trackingNo: 'TRK010', fromCampus: 'Campus S', toCampus: 'Campus T', status: 'Pending transit' },
  { trackingNo: 'TRK011', fromCampus: 'Campus U', toCampus: 'Campus V', status: 'In transit' },
  { trackingNo: 'TRK012', fromCampus: 'Campus W', toCampus: 'Campus X', status: 'Pending delivery' },
  { trackingNo: 'TRK013', fromCampus: 'Campus Y', toCampus: 'Campus Z', status: 'Delivering' },
  { trackingNo: 'TRK014', fromCampus: 'Campus AA', toCampus: 'Campus BB', status: 'Signed' },
  { trackingNo: 'TRK015', fromCampus: 'Campus CC', toCampus: 'Campus DD', status: 'Pending pickup' },
];

// 包裹详情页的模拟数据
const packageDetail = {
  trackingNo: 'TRK001',
  status: 'Pending pickup',
  senderName: 'John Doe',
  senderPhone: '123-456-7890',
  senderAddress: '123 Main St, Campus A',
  receiverName: 'Jane Smith',
  receiverPhone: '987-654-3210',
  receiverAddress: '456 Elm St, Campus B',
  pickupCourierName: 'Mike Johnson',
  pickupCourierEmail: 'mike@example.com',
  pickupCourierPhone: '555-123-4567',
  deliveryCourierName: 'No courier delivers the package.',
  deliveryCourierEmail: '',
  deliveryCourierPhone: '',
};

// 包裹列表页逻辑
if (document.querySelector('.package-list')) {
  let currentPage = 1;
  const itemsPerPage = 10;

  // 渲染包裹列表
  function renderPackageList(filteredPackages = packages) {
    const packageList = document.querySelector('.package-list');
    packageList.innerHTML = '';

    // 计算当前页的数据范围
    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const paginatedPackages = filteredPackages.slice(startIndex, endIndex);

    // 创建表格结构
    const table = document.createElement('table');
    const thead = document.createElement('thead');
    const tbody = document.createElement('tbody');

    // 创建表头
    const headerRow = document.createElement('tr');
    const headers = ['Tracking No', 'From Campus', 'To Campus', 'Status', 'Detail'];
    headers.forEach(headerText => {
      const th = document.createElement('th');
      th.textContent = headerText;
      headerRow.appendChild(th);
    });
    thead.appendChild(headerRow);
    table.appendChild(thead);

    // 创建表格行
    paginatedPackages.forEach(pkg => {
      const row = document.createElement('tr');
      const trackingCell = document.createElement('td');
      trackingCell.textContent = pkg.trackingNo;
      row.appendChild(trackingCell);

      const fromCell = document.createElement('td');
      fromCell.textContent = pkg.fromCampus;
      row.appendChild(fromCell);

      const toCell = document.createElement('td');
      toCell.textContent = pkg.toCampus;
      row.appendChild(toCell);

      const statusCell = document.createElement('td');
      statusCell.textContent = pkg.status;
      row.appendChild(statusCell);

      const detailCell = document.createElement('td');
      const detailButton = document.createElement('button');
      detailButton.textContent = 'Detail';
      detailButton.addEventListener('click', () => {
        window.location.href = 'package-detail.html';
      });
      detailCell.appendChild(detailButton);
      row.appendChild(detailCell);

      tbody.appendChild(row);
    });

    table.appendChild(tbody);
    packageList.appendChild(table);

    // 更新分页按钮
    const pagination = document.querySelector('.pagination');
    pagination.innerHTML = '';

    // 上一页按钮
    const prevButton = document.createElement('button');
    prevButton.innerHTML = '&lt;';
    prevButton.addEventListener('click', () => {
      if (currentPage > 1) {
        currentPage--;
        renderPackageList(filteredPackages);
      }
    });
    pagination.appendChild(prevButton);

    // 页码按钮
    const totalPages = Math.ceil(filteredPackages.length / itemsPerPage);
    for (let i = 1; i <= totalPages; i++) {
      const pageButton = document.createElement('button');
      pageButton.textContent = i;
      if (i === currentPage) {
        pageButton.classList.add('active');
      }
      pageButton.addEventListener('click', () => {
        currentPage = i;
        renderPackageList(filteredPackages);
      });
      pagination.appendChild(pageButton);
    }

    // 下一页按钮
    const nextButton = document.createElement('button');
    nextButton.innerHTML = '&gt;';
    nextButton.addEventListener('click', () => {
      if (currentPage < totalPages) {
        currentPage++;
        renderPackageList(filteredPackages);
      }
    });
    pagination.appendChild(nextButton);
  }

  // 搜索功能
  document.getElementById('searchButton').addEventListener('click', () => {
    const searchTerm = document.getElementById('searchInput').value.toLowerCase();
    const filteredPackages = packages.filter(pkg => 
      pkg.trackingNo.toLowerCase().includes(searchTerm) || 
      pkg.fromCampus.toLowerCase().includes(searchTerm) || 
      pkg.toCampus.toLowerCase().includes(searchTerm)
    );
    currentPage = 1;
    renderPackageList(filteredPackages);
  });

  // 初始渲染
  renderPackageList();
}

// 包裹详情页逻辑
if (document.querySelector('.package-detail')) {
  // 填充包裹基本信息
  document.getElementById('trackingNo').textContent = packageDetail.trackingNo;
  document.getElementById('status').textContent = packageDetail.status;
  document.getElementById('senderName').textContent = packageDetail.senderName;
  document.getElementById('senderPhone').textContent = packageDetail.senderPhone;
  document.getElementById('senderAddress').textContent = packageDetail.senderAddress;
  document.getElementById('receiverName').textContent = packageDetail.receiverName;
  document.getElementById('receiverPhone').textContent = packageDetail.receiverPhone;
  document.getElementById('receiverAddress').textContent = packageDetail.receiverAddress;
  document.getElementById('pickupCourierName').textContent = packageDetail.pickupCourierName;
  document.getElementById('pickupCourierEmail').textContent = packageDetail.pickupCourierEmail;
  document.getElementById('pickupCourierPhone').textContent = packageDetail.pickupCourierPhone;
  document.getElementById('deliveryCourierInfo').textContent = packageDetail.deliveryCourierName;

  // 进度线和状态流程图
  const progressFlow = document.getElementById('progressFlow');
  
  // 创建蓝色进度线
  const progressLine = document.createElement('div');
  progressLine.className = 'progress-line';
  progressFlow.appendChild(progressLine);
  
  // 创建状态流程图
  const statusFlow = document.createElement('div');
  statusFlow.className = 'status-flow';
  
  // 定义包裹状态步骤
  const statusSteps = ['Pending pickup', 'Picked up', 'Pending transit', 'In transit', 'Pending delivery', 'Delivering', 'Signed'];
  
  // 动态生成状态步骤
  statusSteps.forEach((step, index) => {
    const statusStep = document.createElement('div');
    statusStep.className = 'status-step';
    // 高亮当前状态及之前的步骤
    if (index <= statusSteps.indexOf(packageDetail.status)) {
      statusStep.classList.add('active');
    }
    
    const stepText = document.createElement('p');
    stepText.textContent = step;
    statusStep.appendChild(stepText);
    
    statusFlow.appendChild(statusStep);
  });
  
  progressFlow.appendChild(statusFlow);

  // 返回按钮功能
  document.getElementById('backButton').addEventListener('click', () => {
    window.location.href = 'package-list.html';
  });
}